/* Copyright 2007 Gearbox Software - http://www.gearboxsoft.com
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
     http:www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.*/
namespace GearBlog.Web.ConfigSections
{
	using System;
	using System.Collections.Generic;
	using System.Collections.Specialized;
	using System.Configuration;

	public class FileUploadComponentSection:ConfigurationSection
	{
		public class Fields
		{
			public const string Storages = "Storages";
		}
		[ConfigurationProperty(Fields.Storages, IsDefaultCollection = false)]
		[ConfigurationCollection(typeof(StorageCollection))]
		public StorageCollection Storages
		{
			get { return (StorageCollection)this[Fields.Storages]; }
			set { this[Fields.Storages] = value; }
		}
		public static FileUploadComponentSection GetSection()
		{
			return (FileUploadComponentSection) ConfigurationManager.GetSection("FileUploadComponent");
		}
	}
	public class Storage:ConfigurationElement
	{
		public class Fields
		{
			public const string Key = "Key";
			public const string Path = "Path";
			public const string AllowedRoles = "AllowedRoles";
		}
		public Storage()
		{
			
		}
		public Storage(string key)
		{
			Key = key;
		}
		[ConfigurationProperty(Fields.Key)]
		public string Key
		{
			get { return (string) this[Fields.Key];  }
			set { this[Fields.Key] = value; }
		}

		[ConfigurationProperty(Fields.Path)]
		public string Path
		{
			get { return (string) this[Fields.Path]; }
			set { this[Fields.Path] = value; }
		}

		[ConfigurationProperty(Fields.AllowedRoles)]
		public string AllowedRolesString
		{
			get { return (string) this[Fields.AllowedRoles]; }
			set { this[Fields.AllowedRoles] = value; }
		}

		public string [] GetAllowedRoles()
		{
			string[] roles= AllowedRolesString.Split(',');
			List<string> col=new List<string>();
			foreach (string role in roles)
			{
				col.Add(role.Trim());
			}
			return col.ToArray();
		}
	}
	public class StorageCollection:ConfigurationElementCollection
	{
		protected override ConfigurationElement CreateNewElement()
		{
			return new Storage();
		}
		protected override ConfigurationElement CreateNewElement(string elementName)
		{
			return new Storage(elementName);
		}
		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((Storage) element).Key;
		}
		public new Storage this[string key]
		{
			get { return (Storage) base.BaseGet(key); }
		}
	}
}
